package gw.com.android.ui.news;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gwtsz.gts2.hx.R;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

import gw.com.android.app.AppMain;
import gw.com.android.presenter.http.StrategyRequest;
import gw.com.android.ui.views.ExpandableTextView;
import gw.com.android.ui.views.GlideCircleTransform;

/**
 * @author jett
 * @since 2017-11-29.
 */
public class StrategyNewsAdapter extends RecyclerView.Adapter<StrategyNewsAdapter.ItemHolder> {

    private Context context;
    private boolean isNew;
    private List<StrategyRequest.ArticleData> mDatas;

    private DateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());

    StrategyNewsAdapter(Context context) {
        this.context = context;
    }

    public void update(List<StrategyRequest.ArticleData> list, boolean isNew) {
        this.mDatas = list;
        this.isNew = isNew;
        notifyDataSetChanged();
    }

    public void append(List<StrategyRequest.ArticleData> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        if (mDatas == null) {
            mDatas = list;
        } else {
            mDatas.addAll(list);
        }
        notifyDataSetChanged();
    }

    @Override
    public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View root = LayoutInflater.from(context).inflate(R.layout.list_item_strategy_news, parent, false);
        return new ItemHolder(root);
    }

    @Override
    public void onBindViewHolder(ItemHolder holder, int position) {
        StrategyRequest.ArticleData item = getItem(position);
        if (item == null) {
            return;
        }
        holder.isNew.setVisibility(isNew && position == 0 ? View.VISIBLE : View.GONE);
        holder.time.setText(myFormat.format(item.date));
        holder.authorName.setText(item.authorName);
        CharSequence content = Html.fromHtml(item.content);
        if (content != null) {
            String str = content.toString();
            content = str.replaceAll("[\n]+$", "");
            holder.etvContent.setConvertText(true, content);
        }

        Glide.with(context).load(item.iconUrl)
                .placeholder(R.mipmap.a_def_teacher)
                .error(R.mipmap.a_def_teacher)
                .transform(new GlideCircleTransform(context)).into(holder.icon);
    }

    public StrategyRequest.ArticleData getItem(int position) {
        if (mDatas != null && mDatas.size() > position) {
            return mDatas.get(position);
        }
        return null;
    }

    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    static class ItemHolder extends RecyclerView.ViewHolder {

        final ExpandableTextView etvContent;
        private final TextView time;
        private final TextView authorName;
        private final ImageView icon;
        private final ImageView isNew;

        ItemHolder(View itemView) {
            super(itemView);
            etvContent = (ExpandableTextView) itemView.findViewById(R.id.etv_content);
            time = (TextView) itemView.findViewById(R.id.tv_strategy_time);
            authorName = (TextView) itemView.findViewById(R.id.tv_strategy_author_name);
            icon = (ImageView) itemView.findViewById(R.id.iv_strategy_icon);
            isNew = (ImageView) itemView.findViewById(R.id.iv_strategy_isnew);
            etvContent.setExpandCollapseHintText(AppMain.getAppString(R.string.expandable_expand), AppMain.getAppString(R.string.expandable_collapse));
        }

    }

}